import * as types from '../types'
import axios from 'axios'

const state = {
  notes: [],
  total: 0
}

const getters = {
  notes: state => state.notes,
  totalNotes: state => state.total
}

const mutations = {
  [types.NOTES_LOADED](state, data) {
    state.total = data.total || 0
    state.notes = data.notes || []
  }
}

const actions = {
  async searchNotes({
    commit
  }, query) {
    let response = await axios.post('/vts/search/notes', query)
    commit(types.NOTES_LOADED, response.data)
  },
  async saveNote({
    commit
  }, data) {
    let response = await axios.post('/vts/note/save', data.form)
    if (response.data.saved) data.callback()
  },
  async removeNote({
    commit
  }, data) {
    let response = await axios.post('/vts/note/remove', {
      id: data.id
    })
    if (response.data.removed) data.callback()
  },
  async getNote({
    commit
  }, data) {
    let response = await axios.get('/vts/note/get/' + data.id)
    if (response.status === 200) data.callback(response.data)
  }
}

export default {
  state,
  mutations,
  getters,
  actions
}
